package GUI;

import static org.junit.Assert.*;

import java.io.IOException;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;

import junit.framework.TestCase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import client.ClientConsole;

import Server.EchoServer;



public class GUI_ActivityReportTest extends TestCase {

	private GUI_ActivityReport ar;
	private EchoServer es;
	public static void setUpBeforeClass() throws Exception {
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	
	public void setUp() throws Exception
	{
		es=new EchoServer(5555,"root","");
		try {
			es.listen();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		final GUI_LogIn lg=new GUI_LogIn();
		ClientConsole clc=new ClientConsole(lg.host);
		final GUI_ManagerMenu mm=new GUI_ManagerMenu(clc,lg);
		ar=new GUI_ActivityReport(clc,lg,new GUI_Report(clc,lg,mm));
	}

	@After
	public void tearDown() throws Exception {
		es.close();
	}

	@Test
	public void testGetNumOfReadersInActive()
	{	
		PreparedStatement update;
		int before=ar.GetNumOfReadersPerStatus("active");
		try {
			update = EchoServer.conn.prepareStatement("INSERT INTO readercard VALUES(?,?,?,?,?,?,'active');");
			update.setInt(1, 11);
			update.setString(2, "dan");
			update.setString(3,"048764038");
			update.setString(4, "dannnMoo@yahoo.com");
			update.setString(5,"dan");
			update.setString(6,"mor");
			update.execute();
			int after=ar.GetNumOfReadersPerStatus("active");
			boolean expected=false;
			if((before+1)==after) expected=true;
			Assert.assertTrue(expected);
			update=EchoServer.conn.prepareStatement("DELETE FROM readercard WHERE readerId=11;");
			update.execute();
		}catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		}
	}
	
	public void testGetNumOfReadersInFreeze()
	{	
		int before=ar.GetNumOfReadersPerStatus("freeze");
		try {
			PreparedStatement update;
			update = EchoServer.conn.prepareStatement("INSERT INTO readercard VALUES(?,?,?,?,?,?,'freeze');");
			update.setInt(1, 11);
			update.setString(2, "dan");
			update.setString(3,"048764038");
			update.setString(4, "dannnMoo@yahoo.com");
			update.setString(5,"dan");
			update.setString(6,"mor");
			update.execute();
			int after=ar.GetNumOfReadersPerStatus("freeze");
			boolean expected=false;
			if((before+1)==after) expected=true;
			Assert.assertTrue(expected);
			update=EchoServer.conn.prepareStatement("DELETE FROM readercard WHERE readerId=11;");
			update.execute();
		}catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		}
	}
	
	public void testGetNumOfReadersInNotActive()
	{	
		int before=ar.GetNumOfReadersPerStatus("not active");
		try {
			PreparedStatement update;
			update = EchoServer.conn.prepareStatement("INSERT INTO readercard VALUES(?,?,?,?,?,?,'not active');");
			update.setInt(1, 11);
			update.setString(2, "dan");
			update.setString(3,"048764038");
			update.setString(4, "dannnMoo@yahoo.com");
			update.setString(5,"dan");
			update.setString(6,"mor");
			update.execute();
			int after=ar.GetNumOfReadersPerStatus("not active");
			boolean expected=false;
			if((before+1)==after) expected=true;
			Assert.assertTrue(expected);
			update=EchoServer.conn.prepareStatement("DELETE FROM readercard WHERE readerId=11;");
			update.execute();
		}catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		}
	}
	
	public void testAvgOfNumberOfCopiesInLoan()
	{
		double before=ar.AvgOfNumberOfCopiesInLoan("01","2013");
		PreparedStatement update;
		try {
			update = EchoServer.conn.prepareStatement("INSERT INTO readercard VALUES(?,?,?,?,?,?,'active');");
			update.setInt(1, 11);
			update.setString(2, "dan");
			update.setString(3,"048764038");
			update.setString(4, "dannnMoo@yahoo.com");
			update.setString(5,"dan");
			update.setString(6,"mor");
			update.execute();
			
			update = EchoServer.conn.prepareStatement("INSERT INTO loans VALUES(?,?,?,?,?,?,?);");
			update.setInt(1, 9999);
			update.setInt(2,11);
			DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
		    Calendar cal = Calendar.getInstance();
		    String  today = dateFormat.format(cal.getTime());
			update.setString(3,today);
			cal.add(Calendar.DATE, 3);
			String returnDate=dateFormat.format(cal.getTime());
			update.setString(4,returnDate);
			update.setInt(5,6661);
			update.setString(6,"");
			update.setString(7,"");
			update.execute();
			update=EchoServer.conn.prepareStatement("UPDATE copies SET availability=0 WHERE copyId=6661;");
			update.execute();
			
			double expectedRes=(double)((ar.week1+ar.week2+ar.week3+ar.week4+1)/4);
			ar.week1=0;
			ar.week2=0;
			ar.week3=0;
			ar.week4=0;
			double after=ar.AvgOfNumberOfCopiesInLoan("01","2013");
			boolean expected=false;
			if(expectedRes==after) expected=true;
			Assert.assertTrue(expected);
			update=EchoServer.conn.prepareStatement("DELETE FROM loans WHERE loanId=9999;");
			update.execute();
			update=EchoServer.conn.prepareStatement("UPDATE copies SET availability=1 WHERE copyId=6661;");
			update.execute();
			update=EchoServer.conn.prepareStatement("DELETE FROM readercard WHERE readerId=11;");
			update.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void testMedianOfNumberOfCopiesInLoan()
	{
		int before=ar.MedianOfNumberOfCopiesInLoan("01","2013");
		PreparedStatement update;
		try {
			update = EchoServer.conn.prepareStatement("INSERT INTO readercard VALUES(?,?,?,?,?,?,'active');");
			update.setInt(1, 11);
			update.setString(2, "dan");
			update.setString(3,"048764038");
			update.setString(4, "dannnMoo@yahoo.com");
			update.setString(5,"dan");
			update.setString(6,"mor");
			update.execute();
			
			update = EchoServer.conn.prepareStatement("INSERT INTO loans VALUES(?,?,?,?,?,?,?);");
			update.setInt(1, 9999);
			update.setInt(2,11);
			DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
		    Calendar cal = Calendar.getInstance();
		    String  today = dateFormat.format(cal.getTime());
			update.setString(3,today);
			cal.add(Calendar.DATE, 3);
			String returnDate=dateFormat.format(cal.getTime());
			update.setString(4,returnDate);
			update.setInt(5,6661);
			update.setString(6,"");
			update.setString(7,"");
			update.execute();
			update=EchoServer.conn.prepareStatement("UPDATE copies SET availability=0 WHERE copyId=6661;");
			update.execute();
			
			ArrayList<Integer> ToSort=new ArrayList<Integer>(ar.weekRes);
			int day=Integer.parseInt(today.substring(8, 10));
			if(day>=1&&day<=7)
				ToSort.set(0, ToSort.get(0)+1);
			if(day>=8&&day<=14)
				ToSort.set(1, ToSort.get(1)+1);
			if(day>=15&&day<=21)
				ToSort.set(2, ToSort.get(2)+1);
			if(day>=22&&day<=28)
				ToSort.set(3, ToSort.get(3)+1);
			Collections.sort(ToSort);
			int expectedRes=ToSort.get(2);
			ar.week1=0;
			ar.week2=0;
			ar.week3=0;
			ar.week4=0;
			int after=ar.MedianOfNumberOfCopiesInLoan("01","2013");
			boolean expected=false;
			if(expectedRes==after) expected=true;
			Assert.assertTrue(expected);
			update=EchoServer.conn.prepareStatement("DELETE FROM loans WHERE loanId=9999;");
			update.execute();
			update=EchoServer.conn.prepareStatement("UPDATE copies SET availability=1 WHERE copyId=6661;");
			update.execute();
			update=EchoServer.conn.prepareStatement("DELETE FROM readercard WHERE readerId=11;");
			update.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
